package edu.pku.sei.transformation.qvt.adapter.standardqvt;

import java.util.ArrayList;
import java.util.List;

import edu.pku.sei.transformation.editor.model.pattern.Pattern;
import edu.pku.sei.transformation.editor.model.variable.ConnectionVariable;
import edu.pku.sei.transformation.editor.model.variable.ObjectVariable;
import edu.pku.sei.transformation.qvt.rule.IConnectionTemplate;
import edu.pku.sei.transformation.qvt.rule.IObjectTemplate;
import edu.pku.sei.transformation.qvt.rule.IPattern;

public class PatternAdapter implements IPattern {
	protected Pattern element;
	protected List<IConnectionTemplate> connections = new ArrayList<IConnectionTemplate>();
	protected List<IObjectTemplate> nodes = new ArrayList<IObjectTemplate>();
	public PatternAdapter(Pattern p) {
		this.element = p;
		this.addNodes();
		this.addLinks();
	}
	
	protected void addNodes() {
		for (Object o : this.element.getNodes()) {
			if (o instanceof ObjectVariable) {
				ObjectVariable v = (ObjectVariable) o;
				nodes.add(new ObjectTemplateAdapter(v));
			} else {
				// comment, text template
			}
		}
	}
	
	protected void addLinks() {
		for (Object o : this.element.getLinks()) {
			if (o instanceof ConnectionVariable) {
				ConnectionVariable v = (ConnectionVariable) o;
				ConnectionTemplateAdapter con = new ConnectionTemplateAdapter(v);
				for (IObjectTemplate node : nodes) {
					if (node instanceof ObjectTemplateAdapter) {
						ObjectVariable nodeElement = ((ObjectTemplateAdapter) node).element;
						if (nodeElement == v.getSource()) {
							con.source = node;
							node.getOutGoings().add(con);
						} else if (nodeElement == v.getTarget()) {
							con.target = node;
						}
					}
				}
			} else {
				// constraint connection
			}
		}
	}

	@Override
	public List<IConnectionTemplate> getLinks() {
		return connections;
	}

	@Override
	public List<IObjectTemplate> getNodes() {
		return nodes;
	}

}
